package day06;

import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 编写一个程序，将当前目录下所有员工文件进行读取，并解析出所有员工为Emp
 * 对象并存入集合。然后排序该集合，按照员工的年龄排序，年龄大的靠前，年龄小
 * 的靠后。排序完毕后输出结果。
 * @author Bonnie
 *
 */
public class Test11 {
    public static void main(String[] args) throws FileNotFoundException {
        List<Emp> employees = new ArrayList<Emp>();
        File currentDir = new File("Home_Work/src/day06/");
        File[] files = currentDir.listFiles((dir, name) -> name.startsWith("emp张"));
        for (File file : files) {
            try {
                employees.add(getEmp(file));
            } catch (IOException | ParseException e) {
                throw new RuntimeException(e);
            }
        }
        Collections.sort(employees,(Comparator.comparingInt(Emp::getAge).reversed()));
        for (Emp emp : employees) {
            System.out.println(emp);
        }
    }

    private static Emp getEmp(File fname) throws IOException, ParseException {
        FileReader fr = new FileReader(fname);
        BufferedReader read= new BufferedReader(fr);
        String str = null;
        try {
            str = read.readLine();
        }catch (IOException e){
            e.printStackTrace();
        }
        String[] strs = str.split(",");
        String name = strs[0];
        int age = Integer.parseInt(strs[1]);
        String gender = strs[2];
        int salay = Integer.parseInt(strs[3]);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(strs[4]);
        fr.close();
        read.close();
        return new Emp(name,age,gender,salay,date);
    }
}
